import wx
import wx.grid
class MarketDataPanel(wx.Panel):
    def __init__(self, parent):
        super(MarketDataPanel, self).__init__(parent)
        self.init_ui()

    def init_ui(self):
        sizer = wx.BoxSizer(wx.VERTICAL)

        # 市场数据标题
        title = wx.StaticText(self, label="市场数据", style=wx.ALIGN_CENTER)
        title.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        sizer.Add(title, 0, wx.EXPAND | wx.ALL, 5)

        # 市场数据表格
        self.data_grid = wx.grid.Grid(self)
        self.data_grid.CreateGrid(0, 4)
        self.data_grid.SetColLabelValue(0, "时间")
        self.data_grid.SetColLabelValue(1, "代码")
        self.data_grid.SetColLabelValue(2, "价格")
        self.data_grid.SetColLabelValue(3, "成交量")

        sizer.Add(self.data_grid, 1, wx.EXPAND | wx.ALL, 5)

        self.SetSizer(sizer)

    def update_data(self, data):
        rows = self.data_grid.GetNumberRows()
        self.data_grid.InsertRows(rows)
        self.data_grid.SetCellValue(rows, 0, data["timestamp"])
        self.data_grid.SetCellValue(rows, 1, data["symbol"])
        self.data_grid.SetCellValue(rows, 2, str(data["price"]))
        self.data_grid.SetCellValue(rows, 3, str(data["volume"]))
        self.data_grid.MakeCellVisible(rows, 0)